//
//  ChooiceCountryViewController.m
//  iOSBaseFramework
//
//  Created by user on 2020/7/1.
//  Copyright © 2020 com.taylor. All rights reserved.
//

#import "ChooiceCountryViewController.h"
#import "TRFileManageTool.h"
#import "ChooiceCountryCellTableViewCell.h"

@interface ChooiceCountryViewController () <UITableViewDelegate, UITableViewDataSource>


- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
@end

@implementation ChooiceCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *str = [[NSBundle mainBundle] pathForResource:@"country" ofType:@"plist"];
    
    NSMutableDictionary *dataSources = [[NSMutableDictionary alloc] initWithContentsOfFile:str];
    self.keys = dataSources.allKeys;
    self.countrys = dataSources.allValues;
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.allowsSelection = true;
}


-(UITableView *)tableView {
    if (_tableView == NULL) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
//表格组数 Sections 组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.keys count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.countrys[section] count];
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    NSLog(@"section index title: %@ %ld", title, index);
    return index;
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.keys;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChooiceCountryCellTableViewCell *cell = (ChooiceCountryCellTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell == nil){
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
        cell = [[ChooiceCountryCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        [cell init];
    }
    NSDictionary *countryDic = [[self.countrys objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell updateCountry:countryDic[@"country"] Code:countryDic[@"code"]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
